package com.olda.play.system.biz.impl;

import cn.hutool.core.bean.BeanUtil;
import com.olda.play.constant.Constant;
import com.olda.play.enums.ConstantEnum;
import com.olda.play.system.biz.SysDeptBizService;
import com.olda.play.system.biz.dao.SysDeptBizMapper;
import com.olda.play.system.dto.SysDeptDto;
import com.olda.play.system.model.dao.SysDeptMapper;
import com.olda.play.system.model.po.SysDept;
import com.olda.play.system.model.po.SysDeptExample;
import com.olda.play.system.vo.SysDeptVo;
import com.olda.play.utils.UserUtils;
import org.apache.logging.log4j.util.Strings;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;

/**
 */
@Service
public class SysDeptBizServiceImpl implements SysDeptBizService {
    @Autowired
    private SysDeptMapper sysDeptMapper;
    @Autowired
    private SysDeptBizMapper sysDeptBizMapper;

    /**
     * 根据条件查询部门信息
     * @param param
     * @return
     */
    @Override
    public List<SysDeptDto> querySysDeptList( SysDeptVo param) {
        Map<String,Object> map= BeanUtil.copyProperties(param, Map.class);
        List<SysDeptDto> list=sysDeptBizMapper.selectByCondition(map);
        return list;
    }

    /**
     * 根据id查询部门信息
     * @param id
     * @return
     */
    @Override
    public SysDept selectSysDeptById(Long id) {
        if(id==null){
            return null;
        }
        return sysDeptMapper.selectByPrimaryKey(id);
    }
    /**
     * 根据id删除部门层级信息
     * @param id
     */
    @Override
    public void deleteSyDeptById(Long id) {
        //查询所有下级部门
        Map<String, Object> map=new HashMap<>();
        map.put("id",id);

        List<SysDeptDto>  list=sysDeptBizMapper.selectAllChildByParentId(map);
        //删除子级
        list.forEach(item ->{sysDeptMapper.deleteByPrimaryKey(item.getId());});
        //删除部门
        sysDeptMapper.deleteByPrimaryKey(id);

    }
    /**
     * 根据名称查询部门信息
     * @param name
     * @return
     */
    @Override
    public SysDept selectSysDeptByName(String name) {
        SysDeptExample example=new SysDeptExample();
        SysDeptExample.Criteria criteria=example.createCriteria();
        criteria.andNameEqualTo(name);

        List<SysDept> list=sysDeptMapper.selectByExample(example);

        return list.size()>0?list.get(0):null;
    }

    /**
     * 新增一个部门信息
     * @param sysDept
     */
    @Override
    public void save(SysDept sysDept) {
        sysDept.setCreateTime(new Date());
        sysDept.setCreateUser(UserUtils.getUserId().toString());
        sysDept.setCreateUserName(UserUtils.getUsername());
        sysDept.setIsDeleted(ConstantEnum.IS_DELETE_NO.getCode());
        sysDept.setPlatform(ConstantEnum.PLATEFORM_0.getCode());

        // 生成部门树路径
        String treePath = generateDeptTreePath(sysDept);
        sysDept.setTreePath(treePath);

        sysDeptMapper.insert(sysDept);
    }
    /**
     * 更新部门信息
     * @param sysDept
     */
    @Override
    public void updateSysDept(SysDept sysDept) {
        // 生成部门树路径
        String treePath = generateDeptTreePath(sysDept);
        sysDept.setTreePath(treePath);

        sysDept.setModifyTime(new Date());
        sysDept.setModifyUser(UserUtils.getUserId().toString());
        sysDept.setModifyUserName(UserUtils.getUsername());
        sysDeptMapper.updateByPrimaryKeySelective(sysDept);
    }

    /**
     * 生成部门路径
     *
     * @param dept
     * @return
     */
    private String generateDeptTreePath(SysDept dept) {
        Long parentId = dept.getParentId();
        String treePath;
        if (parentId.equals(Constant.ROOT_DEPT_ID)) {
            treePath = String.valueOf(Constant.ROOT_DEPT_ID)+",";
        } else {
            SysDept parentDept = this.selectSysDeptById(parentId);
            treePath = Optional.ofNullable(parentDept).map(item -> item.getTreePath() + item.getId() + ",").orElse(Strings.EMPTY);
        }
        return treePath;
    }

}
